package com.mj.controller.common.config;

import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
import org.springframework.web.servlet.HandlerInterceptor;  
import org.springframework.web.servlet.ModelAndView;

import com.mj.util.StringUtil;  
 
/**
 * 登陆拦截器
 * session中存在就进入请求，不存在直接返回登录页面
 * @author majie
 *
 */
public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		if(!StringUtil.isEmpty(request.getHeader("token"))) {
			//这里可以刷新token失效时间
			return true;
		}
		//判断是否是ajax请求
		if(!StringUtil.isEmpty(request.getHeader("x-requestd-with"))) {
			//第二个参数是登陆页面
			response.setHeader("sessionOutTime", null);
		}else {
			request.getRequestDispatcher("这里是转发路径").forward(request, response);
		}
		return false;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	} 
}